package com.somlsis.fragment

import android.content.Intent
import androidx.fragment.app.Fragment
import com.base.BaseFragment
import com.base.utils.LoadImageUtils
import com.event.MessageEvent
import com.event.RefreshUserEvent
import com.somlsis.activity.HomeActivity
import com.somlsis.activity.RunningActivity
import com.somlsis.bean.UserLatLng
import com.somlsis.databinding.FragmentHomeBinding
import org.greenrobot.eventbus.Subscribe

class HomeFragment(private val listener: HomeActivity.OnHomeAddDeviceNextClickListener) :
    BaseFragment<FragmentHomeBinding>() {
    override fun getViewBinding(): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }

    override fun createView() {
        binding.addDevice.setOnClickListener {
            listener.onAddDeviceClick()
        }
        binding.running.setOnClickListener {
            activity?.let {
                deviceCenter.startRunning()
                it.startActivity(Intent(it, RunningActivity::class.java))
            }
        }
        refreshUI()
    }

    override fun refreshUI() {
        val user = dataCenter.getUser()
        user?.let {
            LoadImageUtils.loadImage(binding.userAvatar, it.phone)
            binding.userName.text = it.name
        }
    }

    @Subscribe
    fun onRefreshUserEvent(event: RefreshUserEvent) {
        refreshUI()
    }

}